<template>
    <div>
        <div>
            <el-input suffix-icon="el-icon-plus"
                      class="addPosInput"
                      size="small"
                      placeholder="添加职位"
                      v-model="pos.name"
                      @keydown.enter.native="addPosition"
            >
            </el-input>
            <el-button type="primary" icon="el-icon-plus" size="small" @click="addPosition">添加</el-button>
        </div>

        <div>
            <el-table
                    border
                    size="small"
                    stripe
                    :data="positions"
                    @selection-change="handleSelectionChange"
                    style="width: 70%">

                <el-table-column
                    type="selection"
                        width="55">
                </el-table-column>

                <el-table-column
                        prop="id"
                        label="编号"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间"
                        width="200">
                </el-table-column>
               <!-- 数据库返回的是boolean类型，是不会直接显示的-->
                <el-table-column
                        prop="enabled"
                        label="是否启用"
                        width="120">
                        <template  slot-scope='scope'>
                            <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                            <el-tag type="danger" v-else>未启用</el-tag>
                        </template>
                </el-table-column>



                <el-table-column label="操作">
                    <template scope="scope">
                        <el-button
                                size="small"
                                @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="small"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>



            </el-table>
        </div>

        <el-button @click="deleteMany" type="danger" size="small" style="margin-top: 8px"
                   :disabled="this.multipleSelection.length==0">批量删除</el-button>

        <el-dialog
                title="编辑职位"
                :visible.sync="dialogVisible"
                width="30%"
               >

                <table>
                    <tr>
                        <td>
                            <el-tag>
                                职位名称
                            </el-tag>
                        </td>
                        <td>
                            <el-input v-model="updatePos.name"></el-input>
                        </td>
                    </tr>


                    <tr>
                        <td>
                            <el-tag>
                                是否启用
                            </el-tag>
                        </td>
                        <td>
                            <el-switch align="left"
                                    size="small"
                                    v-model="updatePos.enabled"
                                    active-color="#13ce66"
                                    active-text="已启用"
                                    inactive-text="未启用">
                            </el-switch>
                        </td>

                    </tr>
                </table>


            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false" size="small">取 消</el-button>
    <el-button type="primary" @click="doUpdate" size="small">确 定</el-button>
  </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "PosMana",
        data(){
            return{
                pos:{
                    name:''
                },
                positions:[],
                dialogVisible:false,
                updatePos:{
                    name:"",
                    enabled:false
                },
                multipleSelection:[]

            }
        },

        mounted() {
            this.initPositions();
        },

        methods:{
            initPositions(){
                this.getRequest('/system/basic/pos/').then(resp=>{
                    if (resp){
                        this.positions = resp;
                    }
                })
            },
            doUpdate(){
               this.putRequest('/system/basic/pos/',this.updatePos).then(resp=>{
                   if (resp){
                       this.initPositions();
                       this.dialogVisible=false;
                   }
               })
            },

            addPosition(){
              if (this.pos.name){
                  this.postRequest('/system/basic/pos/',this.pos).then(resp=>{
                      if (resp){
                          this.initPositions();
                      }
                  })
              }else{
                  this.$message.error('职位名称不能为空！');
              }
            },

            showEditView(index, date) {
                Object.assign(this.updatePos,date);/*date拷贝给updatePos*/
               this.updatePos.createDate="";
              this.dialogVisible=true;
            },

            handleDelete(index, data) {
                this.$confirm('此操作将永久删除['+data.name+'], 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                   this.deleteRequest('/system/basic/pos/'+data.id).then(resp=>{
                       if (resp){
                           this.initPositions();
                       }
                   });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val){
                this.multipleSelection=val;

            },
            deleteMany(){
                this.$confirm('此操作将永久删除['+this.multipleSelection.length+']条职位, 是否继续?', '提示',
                    {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids ='?';
                    this.multipleSelection.forEach(item=>{
                        ids += 'ids=' + item.id+'&';
                    });
                    this.deleteRequest('/system/basic/pos/'+ids).then(resp=>{
                        if (resp){
                            this.initPositions();
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            }
        }
    }
</script>

<style>
    .addPosInput{
        width:300px;
        margin-right: 8px;
    }
    /*.posMainMain{
        margin-top: 10px;
    }*/
    .updatePosInput{
        width: 200px;
        margin-left: 8px;
    }
</style>
